package com.mt.fpb.utils;

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
@Slf4j
public class FileUtils {
    @Value("${system.update.imgpath}")
    private String imgpath;


    /**
    *  上传图片
    * */

    public  String  uploadFile(MultipartFile file)  {
        //获取文件名
        String fileName=file.getOriginalFilename();
        //获取文件后缀
        String suffixName=fileName.substring(fileName.lastIndexOf('.'));
        //生成新文件名
        fileName= IdUtil.simpleUUID()+suffixName;
        //上传的文件位置
        String uploadpath=imgpath+fileName;
        //创建文件及其父目录
        File de=cn.hutool.core.io.FileUtil.touch(uploadpath);
        try{
            file.transferTo(de);
            log.info("上传后的图片路径为:"+uploadpath);
            return  "upload success";
        }catch (IOException e){
        }
        return  " upload fail";
    }

}
